﻿using autofac.test.Extend;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace autofac.test.aop
{
    public class autofacAop : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {

            if (invocation.Method.IsDefined(typeof(CacheAttribute), false))
            {
                
                var cachingAttribute = (CacheAttribute)invocation.Method.GetCustomAttributes(typeof(CacheAttribute), false)[0];
                if (cachingAttribute.MethodType == CacheMethod.get)
                {
                    //处理缓存信息
                    invocation.ReturnValue = "ttt";
                }
                else if (cachingAttribute.MethodType == CacheMethod.put)
                {

                }
                else if (cachingAttribute.MethodType == CacheMethod.remove)
                {

                }
            }
            else
            {
                invocation.Proceed();
            }
           
        }
    }


    public interface IA
    {
        [Cache(CacheMethod.get)]
        string Show(int id, string name);

        [Cache(CacheMethod.put)]
        string SaveData(int id, string name);


        [Cache(CacheMethod.remove)]
        void DelData(int id, string name);
    }

    [Intercept(typeof(autofacAop))]
    public class A : IA
    {

        [Cache(CacheMethod.get)]
        public string  Show(int id, string name)
        {
            return "sss";
        }


        [Cache(CacheMethod.put)]
        public string SaveData(int id, string name)
        {
            return "sss";
        }

        [Cache(CacheMethod.remove)]
        public void DelData(int id, string name)
        {

        }

    }

}
